package com.myGisChannel.utils;
import java.io.InputStream;

import java.util.Properties;
import org.apache.log4j.Logger;

/**
 * AppPropertiesUtil: Recupera una instancia del archivo de propiedades.
 *
 */
public class AppPropertiesUtil {

	private static AppPropertiesUtil instance = null;
	private static String PROPERTIES_FILE = "mygis.properties";
	
	private static Logger _log = Logger.getLogger(AppPropertiesUtil.class);
	
	private static Properties properties = null;
	 
	private AppPropertiesUtil(){}
	
	public static AppPropertiesUtil getInstance() {
		if( instance == null){
			loadInstance();
		}
		return instance;
	}
	
	private synchronized static void loadInstance() {
		if( instance != null){
			return;
		}
                
		String fileProperties = PROPERTIES_FILE ;
		instance = new AppPropertiesUtil();
		try {
			properties = new Properties();
            InputStream is = AppPropertiesUtil.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE);
			if (is == null) {
				is = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_FILE);
			}

			properties.load(is);
		} catch (Throwable e) {
			if (_log.isDebugEnabled()) {
				_log.debug( PROPERTIES_FILE + " not found", e);
			}
		}
	}

	public String getValue(String key){
		return getValue(key, null);
	}
	
	public String getValue(String key, String defaultValue){
		String result = properties.getProperty(key, defaultValue);
		return result;
	}

}